//
//  CollectionArticlesViewController.m
//  chanyou
//
//  Created by leon on 15/1/8.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "CollectionArticlesViewController.h"
#import "ArticlesDatabase.h"
#import "ArticlesModel.h"
#import "CollectionArticlesTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ArtDetailViewController.h"
#import <ShareSDK/ShareSDK.h>
@interface CollectionArticlesViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation CollectionArticlesViewController
- (void)dealloc
{
    [_collectArticlesArray release];
    [_collectArticlesView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectArticlesArray = [NSMutableArray array];
        
        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
            if (result) {
                NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
                // 注销之后要将此处清除
                [def setObject:[userInfo uid] forKey:@"user"];
            }
        }];
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        NSArray *articlesArray = [ArticlesDatabase searchAllArticlesWithWeiboId:[[def objectForKey:@"user"] integerValue]];
//        if (weiboId == [[def objectForKey:@"user"] integerValue]){
            [self.collectArticlesArray removeAllObjects];
            [self.collectArticlesArray addObjectsFromArray:articlesArray];
//        }
        [self.collectArticlesView reloadData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}


- (void)createView
{
    self.collectArticlesView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height * 3 / 9 - 110)];
    _collectArticlesView.delegate = self;
    _collectArticlesView.dataSource = self;
    _collectArticlesView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_collectArticlesView registerClass:[CollectionArticlesTableViewCell class] forCellReuseIdentifier:@"collectArticlesCell"];
    [self.view addSubview:_collectArticlesView];
    [_collectArticlesView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_collectArticlesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionArticlesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectArticlesCell"];
    ArticlesModel *articles = [[ArticlesModel alloc] init];
    articles = [_collectArticlesArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = articles.name;
    [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:articles.image_url] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArtDetailViewController *articlesDetail = [[ArtDetailViewController alloc] init];
    ArticlesModel *articles = [[ArticlesModel alloc] init];
    articles = [_collectArticlesArray objectAtIndex:indexPath.row];
    articlesDetail.detailId = articles.sectionId;
    [self.navigationController pushViewController:articlesDetail animated:YES];
    [articlesDetail release];
}

#pragma mark -
#pragma mark 滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticlesModel *articles = [_collectArticlesArray objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        [ArticlesDatabase deleteFromArticles:articles.sectionId weiboId:[[def objectForKey:@"user"] integerValue]];
        [_collectArticlesArray removeObjectAtIndex:indexPath.row];
    }
    [tableView reloadData];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
